package finetune

import (
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/openai"
)

func RemoteFineTuneList() (code.CODENO, []map[string]any) {
	rcode, res := openai.RemoteApi(openai.OPENAI_FINE_TUNE_LIST, "", nil)
	if rcode != code.SUCCESS {
		return rcode, []map[string]any{}
	}
	dataarr, ok := res.Data.([]any)
	if !ok {
		return rcode, []map[string]any{}
	}
	datam := []map[string]any{}
	for _, v := range dataarr {
		obj := v.(map[string]any)
		mname := obj["fine_tuned_model"].(string)
		datam = append(datam, map[string]any{
			"BaseModel": obj["model"].(string),
			"Model":     mname,
			"Desc":      "自定义模型 " + mname,
			"Avatar":    "",
		})
	}
	return rcode, datam
}

var _tmp_fine_list []map[string]any = []map[string]any{}

func FineTuneList(force bool) (code.CODENO, []map[string]any) {
	if len(_tmp_fine_list) > 0 && force == false {
		return code.SUCCESS, _tmp_fine_list
	}
	cnf := helper.Config().Sub("ChatGPT")
	dataa := cnf.Get("models")
	if dataa != nil && force == false {
		if arr, ok := dataa.([]any); ok {
			for _, val := range arr {
				if v, ok := val.(map[string]any); ok {
					_tmp_fine_list = append(_tmp_fine_list, map[string]any{
						"BaseModel": v["basemodel"],
						"Model":     v["model"],
						"Desc":      v["desc"],
						"Avatar":    v["avatar"],
					})
				}
			}
			return code.SUCCESS, _tmp_fine_list
		}
	}

	rcode, data := RemoteFineTuneList()
	defaultm := map[string]any{
		"BaseModel": "davinci",
		"Model":     "text-davinci-003",
		"Desc":      "问答 Completion",
		"Avatar":    "avatar-openai",
	}
	data = append([]map[string]any{defaultm}, data...)
	_tmp_fine_list = data
	return rcode, data
}
